package org.aristotle.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aristotle.common.response.ResponseEntity;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.aristotle.user.model.entity.UserRole;
import org.aristotle.user.service.IUserRoleService;
import org.aristotle.user.model.vo.UserRoleVO;

@RestController
@RequestMapping("userRole")
@Api(value="用户角色关联controller",tags={"用户角色关联操作接口"})
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    /*分页查询*/
    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "用户角色关联id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "roleId", paramType = "query", value = "角色Id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "userId", paramType = "query", value = "用户Id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "current", paramType = "query", value = "页码", dataType = "Integer", required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页距", dataType = "Integer", required = false,defaultValue = "10")
    })
    public Result<Page<UserRoleVO>> queryPageList(
            @RequestParam(name = "id",required = false) Long id,
            @RequestParam(name = "roleId",required = false) Long roleId,
            @RequestParam(name = "userId",required = false) Long userId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<UserRoleVO> result = userRoleService.queryPageList(
            id,
            roleId,
            userId,
            current,size);
        return ResponseEntity.ok(result);
    }

    @GetMapping
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "用户角色关联id", dataType = "Long", required = true)
    })
    public Result<?> queryById(@RequestParam(name = "id") Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        UserRoleVO result = userRoleService.getById(id);
        if (result == null) {
            return ResponseEntity.notFound();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("获取列表")
    public Result<?> queryList() {
        return ResponseEntity.ok(AristotleBeanUtils.copyListProperties(userRoleService.list(),UserRoleVO::new));
    }

    @PostMapping
    @ApiOperation("添加")
    public Result<?> add(@RequestBody UserRoleVO userRoleVO) {
        if (
            userRoleVO.getRoleId() == null
            || userRoleVO.getUserId() == null
        ) {
            return ResponseEntity.badRequest();
        }
        UserRole userRole = AristotleBeanUtils.transitionBean(userRoleVO, UserRole::new);
        userRole.setId(null);
        userRoleService.save(userRole);
        return ResponseEntity.addOk(userRoleVO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<?> edit(@RequestBody UserRoleVO userRoleVO) {
        if (userRoleVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        if (
            userRoleVO.getRoleId() == null
            || userRoleVO.getUserId() == null
        ) {
            return ResponseEntity.badRequest();
        }
        UserRole userRole = AristotleBeanUtils.transitionBean(userRoleVO, UserRole::new);
        userRoleService.updateById(userRole);
        return ResponseEntity.updateOk(userRoleVO);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "用户角色关联id", dataType = "Long", required = true)
    })
    public Result<?> delete(@PathVariable Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        userRoleService.removeById(id);
        return ResponseEntity.deleteOk(id);
    }

}
